<template>
  <div class="goods-info">
    <van-icon name="arrow-left" size="28px" @click="returnPre" />
    <div class="info-top">
      <img :src="goodsData.imgUrl" />
      <h2>{{goodsData.name}}</h2>
      <p><span>月售{{goodsData.sellCount}}份&nbsp;&nbsp;</span><span>好评率{{goodsData.rating}}%</span></p>
      <p>￥<span>{{goodsData.price}}</span></p>
      <div class="add-subtract-box">
        <div v-if="goodsData.num" class="add-subtract">
          <em @click="addSubtract(goodsData,-1)">-</em>
          <span>{{goodsData.num}}</span>
          <em @click="addSubtract(goodsData,1)">+</em>
        </div>
        <div v-else class="cart" @click="addSubtract(goodsData,1)">
          <van-icon name="cart" />加入购物车
        </div>

      </div>
    </div>
    <div class="goods-desc">
      <h3>商品介绍</h3>
      <p>{{goodsData.goodsDesc}}</p>
    </div>
    <div class="goods-ratings">
      <van-tabs type="card" @change="onChange" v-model="ractive" color="#ffc300" title-active-color="#000">
        <van-tab :title="'全部'+ratingsData.length"></van-tab>
        <van-tab :title="'推荐'+ratingsLength"></van-tab>
        <van-tab :title="'吐槽'+(ratingsData.length-ratingsLength)"></van-tab>
      </van-tabs>
      <ul>
        <li v-for="(item,index) in ratingsData" :key="index">
          <template>
            <img :src="item.avatar">
            <div>
              <p>
                <span>{{item.username}}</span>
                <span>{{item.rateTime}}</span>
              </p>
              <p>
                {{item.text}}
              </p>

            </div>
          </template>
        </li>
      </ul>
    </div>
  </div>

</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import moment from "moment";
@Component({})
export default class extends Vue {
  ractive = 0;
  goodsData: any = {};
  ratingsData: any = [];
  cratings: any = []; //一个过渡项
  addSubtract(val, n) {
    val.num += n;
    this.$store.commit("countFood", { id: val.id, n: n });
  }
  created() {
    //params接收数据
    this.goodsData = this.$route.params;
    this.cratings = this.goodsData.ratings;
    this.cratings.forEach((item: any) => {
      item.rateTime = moment(item.rateTime).format("YYYY-MM-DD HH:mm:ss");
    });
    this.ratingsData = this.cratings;
  }
  get ratingsLength() {
    return this.cratings.filter((item: any) => {
      return item.rateType === 0;
    }).length;
  }
  onChange(index) {
    console.log(index);
    switch (index) {
      case 0:
        this.ratingsData = this.cratings;
        break;
      case 1:
        this.ratingsData = this.cratings.filter((item: any) => {
          return item.rateType == 0;
        });
        break;
      case 2:
        this.ratingsData = this.cratings.filter((item: any) => {
          return item.rateType == 1;
        });
        break;
    }
  }
  returnPre() {
    this.$router.go(-1);
  }
}
</script>

<style lang="scss" scoped>
.goods-info {
  background-color: #f4f5f7;
  .van-icon-arrow-left {
    position: absolute;
    z-index: 888;
    top: 10px;
    left: 10px;
    color: #000;
    font-weight: bold;
  }
}
.info-top {
  margin-bottom: 30px;
  background-color: #fff;
  padding-bottom: 20px;
  position: relative;
  img {
    width: 100%;
    height: 260px;
    margin-bottom: 15px;
  }
  h2,
  p {
    margin-left: 15px;
  }
  p {
    line-height: 36px;
  }
  p:first-of-type {
    color: #999999;
    font-size: 16px;
  }
  p:last-of-type {
    color: #ff3300;
    font-weight: bold;
    span {
      font-size: 30px;
    }
  }
  .add-subtract-box {
    position: absolute;
    bottom: 10%;
    right: 5%;
    .cart {
      width: 140px;
      height: 50px;
      font-size: 16px;
      font-weight: 550;
      border-radius: 25px;
      text-align: center;
      line-height: 50px;
      background-color: #ffc300;
      color: #000;
      .van-icon-cart {
        font-size: 20px;
        margin-right: 4px;
      }
    }
    .add-subtract {
      display: flex;
      align-items: center;
      font-weight: bold;
      em {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        font-size: 28px;
        text-align: center;
      }
      span {
        margin: 0 6px;
        font-size: 20px;
      }
      em:last-of-type {
        background-color: #ffcc33;
        line-height: 30px;
      }
      em:first-of-type {
        border: 1px solid #ffcc33;
        line-height: 22px;
      }
    }
  }
}
.goods-desc {
  padding: 15px;
  background-color: #fff;
  margin-bottom: 30px;
  h3 {
    font-weight: normal;
    line-height: 40px;
  }
  p {
    line-height: 24px;
  }
}
.goods-ratings {
  background-color: #fff;
}

.goods-ratings {
  background-color: #fff;
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  .van-tabs {
    padding-bottom: 10px;
  }
  ul {
    flex: 1;
    overflow-y: scroll;
    li {
      display: flex;
      margin: 0 10px;
      padding: 20px 0;
      border-bottom: 1px solid #f4f4f4;
      img {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        margin-right: 10px;
      }
      div {
        flex: 1;
        p:first-child {
          font-size: 14px;
          display: flex;
          justify-content: space-between;
          span:first-child {
            font-weight: bold;
          }
          span:last-child {
            color: #999999;
          }
        }
        p:nth-child(2) {
          line-height: 30px;
          span:last-child {
            margin-left: 10px;
            color: #999999;
            font-size: 12px;
          }
        }
        p:nth-child(3) {
          font-size: 15px;
          line-height: 20px;
        }
      }
    }
  }
}
</style>